<template>
  <vxe-modal @close="cancel" title="选择工法" v-model="QyGfDuoX" width="80%" append-to-body showFooter esc-closable
             show-zoom resize>
    <div style="height: 75vh; overflow-y: auto;">
      <!-- 顶部搜索 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="工法名称" prop="gfName">
          <el-input size="mini"
                    v-model="queryParams.gfName"
                    placeholder="请输入工法名称"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="年度" prop="nd">
          <el-input size="mini" type="number"
                    v-model="queryParams.nd"
                    placeholder="请输入年度"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="工法类别" prop="gfLb">
          <el-select @change="getZyFlQuery" size="mini" v-model="queryParams.gfLb" placeholder="请选择工法类别" clearable>
            <el-option
              v-for="dict in dict.type.qygf_gflb"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="专业分类" prop="zyFl">
          <el-select v-if="queryParams.gfLb == null" size="mini" v-model="queryParams.zyFl" placeholder="请先选择工法类别"
                     clearable>
          </el-select>
          <el-select v-if="queryParams.gfLb == '房屋建筑工程'" size="mini" v-model="queryParams.zyFl"
                     placeholder="请选择专业分类" clearable>
            <el-option
              v-for="dict in dict.type.qygf_zyfl_fwjzgc"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <el-select v-if="queryParams.gfLb == '土木工程'" size="mini" v-model="queryParams.zyFl"
                     placeholder="请选择专业分类" clearable>
            <el-option
              v-for="dict in dict.type.qygf_zyfl_tmgc"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <el-select v-if="queryParams.gfLb == '工业安装工程'" size="mini" v-model="queryParams.zyFl"
                     placeholder="请选择专业分类" clearable>
            <el-option
              v-for="dict in dict.type.qygf_zyfl_gyazgc"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="申报级别" prop="sbJb">
          <el-select size="mini" v-model="queryParams.sbJb" placeholder="请选择申报级别" clearable>
            <el-option
              v-for="dict in dict.type.qygf_sbjb"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工法完成人" prop="zyWcR">
          <el-input size="mini"
                    v-model="queryParams.zyWcR"
                    placeholder="请输入工法完成人"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="申报单位" prop="sbDw">
          <el-select size="mini" v-model="queryParams.sbDw" placeholder="请选择申报单位" clearable>
            <el-option
              v-for="dict in dict.type.xmgl_lxwj_pzdw"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="专业领域" prop="zyLy">
          <el-select size="mini" v-model="queryParams.zyLy" placeholder="请选择专业领域" clearable>
            <el-option
              v-for="dict in dict.type.qygf_zyly"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table @row-click="clickRow" ref="table" :row-key="(row)=>{ return row.qygfId}" :reserve-selection="true"
                highlight-current-row :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                v-loading="loading" :data="qygfList" @selection-change="handleSelectionChange">
        <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
        <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
        <el-table-column label="年度" align="center" prop="nd" width="70"/>
        <el-table-column label="工法名称" align="center" prop="gfName">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleButtonClick(scope.row)"
            >
              <el-link type="primary">{{ scope.row.gfName }}</el-link>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="申报级别" align="center" prop="sbJb">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.qygf_sbjb" :value="scope.row.sbJb"/>
          </template>
        </el-table-column>
        <el-table-column label="工法类别" align="center" prop="gfLb">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.qygf_gflb" :value="scope.row.gfLb"/>
          </template>
        </el-table-column>
        <el-table-column label="专业分类" align="center" prop="zyFl"/>
        <el-table-column label="工法完成人" align="center" prop="zyWcR"/>
        <el-table-column label="申报单位" align="center" prop="sbDw">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.xmgl_lxwj_pzdw" :value="scope.row.sbDw"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="processStatus" width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.qygf_lczt" :value="scope.row.processStatus"/>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button size="small" @click="submitForm">
        <svg-icon icon-class="confirm1" class-name='custom-class'/>
        确 定
      </el-button>
      <el-button size="small" @click="cancel">
        <svg-icon icon-class="cancel1" class-name='custom-class'/>
        取 消
      </el-button>
    </div>

    <!-- 企业工法详情 -->
    <QyGfXq ref="qyGfXq"/>

  </vxe-modal>
</template>

<script>
import {listQygf} from "@/api/kjcg/qygf";
import QyGfXq from "@/views/kjcg/qygfsb/components/QyGfXq.vue";

export default {
  name: "QyGfDuoX", //企业工法多选
  components: {QyGfXq},
  dicts: ['xmgl_lxwj_pzdw', 'qygf_sbjb', 'qygf_zyly', 'qygf_gflb', 'qygf_zyfl_fwjzgc', 'qygf_zyfl_tmgc', 'qygf_zyfl_gyazgc', 'qygf_lczt'],
  inject: ['updateQyGfLabel'],
  data() {
    return {
      // 遮罩层
      loading: true,
      QyGfDuoX: null,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gfName: null,
        nd: null,
        gfLb: null,
        zyFl: null,
        sbJb: null,
        zyWcR: null,
        kjryId: null,
        sbDw: null,
        zyLy: null,
        xmsbId: null,
        kyXm: null,
        idWhere: null,
        userId: null,
        deptId: null,
        processStatus: '3',
      },
      // 企业工法表格数据
      qygfList: [],
      qygfLable: null,
      getQygfList: null,
      // 总条数
      total: 0,
    }
  },
  created() {

  },
  methods: {
    //打开企业工法详情
    handleButtonClick(row) {
      this.$refs.qyGfXq.handleOpen(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.qygfId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.getQygfList = selection.map(item => ({
        nd: item.nd,          //年度
        gfName: item.gfName,  //工法名称
        sbJb: item.sbJb,      //申报级别
        gfLb: item.gfLb,      //工法类别
        sbDw: item.sbDw,      //申报单位
        qygfId: item.qygfId,  //企业工法ID
      }))
    },
    /** 查询企业工法列表 */
    getList() {
      this.loading = true;
      listQygf(this.queryParams).then(response => {
        this.qygfList = response.rows;
        this.total = response.total;
        this.loading = false;

        // 默认选中已有的数据
        this.$nextTick(() => {
          this.qygfLable.forEach(item => {
            const selectedRow = this.qygfList.find(row => row.qygfId == item.qygfId)
            if (selectedRow) {
              this.$refs.table.toggleRowSelection(selectedRow, true);
            }
          });
        });

      });
    },
    handleOpen(data){
      // 接收来自父组件的数据
      this.qygfLable = data
      this.QyGfDuoX = true;
      this.getList();
    },
    submitForm(){
      this.updateQyGfLabel(this.getQygfList)
      this.QyGfDuoX = false;
      this.$refs.table.clearSelection();
    },
    cancel() {
      this.QyGfDuoX = false;
      this.$refs.table.clearSelection();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeShTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    //当搜索栏中更换工法类别时, 重置专业分类
    getZyFlQuery() {
      this.queryParams.zyFl = null
    },

  }
}
</script>

<style scoped>

</style>
